package weifei.live.simulation;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
// import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.wltea.analyzer.dic.CustomDictionary;
import org.wltea.analyzer.dic.Dictionary;
import weifei.live.simulation.im.entity.LiveStreamBulletSensitiveWord;
import weifei.util.RSAUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Random;
import java.util.Scanner;


@Slf4j
@SpringBootApplication
// @EnableJms
@EnableTransactionManagement
@EnableScheduling
@EnableCaching
@ServletComponentScan
@MapperScan({
        "com.baomidou.mybatisplus.samples.quickstart.mapper",
        "weifei.web.mapper",
        "weifei.mall.mapper",
        "weifei.live.simulation.mapper",
        "weifei.live.simulation.im.mapper"
})
@ComponentScan({
        "weifei.web",
        "weifei.web.service",
        "weifei.web.controller",
        "weifei.mall",
        "weifei.mall.entity",
        "weifei.mall.service",
        "weifei.mall.controller",
        "weifei.live.simulation",
        "weifei.live.simulation.entity",
        "weifei.live.simulation.service",
        "weifei.live.simulation.controller",
        "weifei.live.simulation.im",
        "weifei.live.simulation.im.entity",
        "weifei.live.simulation.im.service",
        "weifei.live.simulation.im.controller"
})
@EnableKnife4j
public class Application
{
    public static ConfigurableApplicationContext applicationContext;


    //length用户要求产生字符串的长度
    public static String getRandomString(int length)
    {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()_+-=|[]{};:,<.>?";
        Random random = new Random();
        var random_bound = str.length() - 1;
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<length; i++)
        {
            int number = random.nextInt(random_bound);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    public static void main(String[] args)
            throws Exception
    {
        // var public_key =
        //         "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAshI07EeH7ttFDHefKKn6zBp0v+Rlc+fW1Q0h9qukHoy1tlOnukih+lvI53vgc08d21mC7vuJpGjkLn6gptNFx4lXnjuCc+cLs50UpTZqLY8f69v63tnZOi3nVVuWoE6d5TWr21iSSOaW7Xj4k50XEu5VeobOSEQtWMHuQx3Zw6kWkHrXdiS/4Uvu6SSB61fprpPW3E5OLPgNipaDmwdqjpP+jSDQF8tgaGigEEIF+1olWicMQLULB9E1tXi4U0yMNo5idyCFrBsc+momMUlwrOkrU8GGV+18IeLf4F4+Vibux97RRbj1fBz58ByCxJQ1+khrXOgrJ+N/KP++qqUbpQIDAQAB";

        // var password = Application.getRandomString(20);
        // var 加密后的密码 = RSAUtils.encryptByPublicKey(password, public_key);
        // System.out.println("\nciphertext:\n" + 加密后的密码);
        // System.out.println("\ninput the running password please.");

        // Scanner scanner = new Scanner(System.in);
        // String input = null;
        // if (scanner.hasNextLine())
        //     input = scanner.nextLine();
        // if (input == null)
        // {
        //     System.out.println("error password");
        //     System.exit(0);
        // }

        // if (input.equals(password))
        // {
        //     System.out.println("error password");
        //     System.exit(0);
        // }
        Application.applicationContext = SpringApplication.run(Application.class, args);


        // 初始化 敏感词 词库
        File dic_file = new File("./sensitive_words.txt");
        if (dic_file.exists())
            dic_file.delete();
        dic_file.createNewFile();

        // 数据库中的敏感词
        var list = new LiveStreamBulletSensitiveWord().selectAll();

        // 自带的敏感词
        var input_dic1 =
                Application.class.getClassLoader().getResourceAsStream("/dic/banned_words.txt");
        var input_dic2 =
                Application.class.getClassLoader().getResourceAsStream("/dic/banned_words_sms.txt");


        var dic_output = new FileOutputStream(dic_file);
        for (var bsw : list)
        {
            var word = ( bsw.getWord() + "\n");
            dic_output.write(word.getBytes());
        }
        if (input_dic1 != null) {
            dic_output.write(input_dic1.readAllBytes());
        }
        if (input_dic2 != null) {
            dic_output.write(input_dic2.readAllBytes());
        }
        dic_output.close();


        // 加载 敏感词 词库
        Dictionary dictionary = Dictionary.getInstance();
        if (!dictionary.isLoadedCustomDictionary()) {
            // 没有加载自定义词典，加载自定义词典
            // https://github.com/renfei/dict
            CustomDictionary customDictionary = new CustomDictionary();
            customDictionary.setMainDictInputStream(new FileInputStream(dic_file));
            dictionary.loadCustomDictionary(customDictionary);
        }
    }

}


